package com.lagou.service.impl;

import com.lagou.client.MailClient;
import com.lagou.dao.AuthCodeDao;
import com.lagou.pojo.AuthCode;
import com.lagou.service.AuthCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AuthCodeServiceImpl implements AuthCodeService {

    @Autowired
    private AuthCodeDao authCodeDao;
    @Autowired
    private MailClient mailClient;

    @Override
    public void addAuthCode(String email, String code) {
        AuthCode authCode = new AuthCode();
        authCode.setEmail(email);
        authCode.setCode(code);
        //计算创建时间及过期时间（10分钟）
        Date createTime = new Date();
        Date expireTime = new Date(createTime.getTime() + 10 * 60 * 1000);
        authCode.setCreateTime(createTime);
        authCode.setExpireTime(expireTime);
        //持久化
        authCodeDao.save(authCode);
        //发邮件
        mailClient.senMail(email, code);
    }
}
